// pages/orederDetails/orederDetails.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      options: options
    })
    // that.getData();
    console.log()

  },

  //确认收货
  confirmOrder: function (event) {
    var that = this;
    var orderId = event.currentTarget.dataset.orderid;
    wx.showModal({
      title: '提示',
      content: '您确认收货吗',
      success: function (res) {
        if (res.confirm) {
          app.getLoginSession(function (session) {
            wx.request({
              url: app.api,
              data: {
                app: 'buyer_order',
                act: 'confirm_order',
                order_id: orderId,
                t_session: session
              },
              success: function (res) {
                var data = res.data;
                console.log(data);
                if (data.done) {
                  wx.showToast({
                    title: data.retval.msg,
                    icon: 'success',
                    duration: 2000,
                    success: function () {
                      wx.navigateBack();  //返回上一层
                    }
                  })
                }
              }
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  cont: function (all, parameter) {
    var value = 0;
    if (parameter == 'discount_price') {
      for (var x in all) {
        value += parseFloat(all[x][parameter] * all[x]['quantity']);
      }
      value = value.toFixed(2);
      console.log(value);
    }
    else {
      for (var x in all) {
        value += parseFloat(all[x][parameter]);
      }
      if (parameter == 'shipping_price') {
        value = value.toFixed(2);
        console.log('sda');
      }
    }
    console.log(value);
    return value;
  },

  getData: function () {
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({

        // 请求地址
        url: app.api,

        // 请求参数
        data: {
          app: 'buyer_order',
          act: 'view',
          order_id: that.data.options.order_id,
          t_session: session
        },

        // 请求成功
        success: function (res) {
          var data = res.data;
          if (data.done && data.retval) { // 成功
            that.setData({
              order: data.retval,

              shipping_fee: that.cont(data.retval.goods, 'shipping_price'),

              num: that.cont(data.retval.goods, 'quantity'),

              goods_money: that.cont(data.retval.goods, 'discount_price'),

            })
          }
        },
        fail: function (res) {
          that.setData({
            loadTips: data.msg || '请求错误，act：index'
          })
        }
      })
    })
  }
})